package com.longyiqun.cm.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.longyiqun.cm.JekinsServer2;
import com.longyiqun.cm.Jobs;
import com.longyiqun.cm.domain.*;
import com.longyiqun.cm.jobdetail2;
import com.longyiqun.cm.mapper.ServerMapper;
import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.model.Job;
import com.offbytwo.jenkins.model.JobWithDetails;
import com.offbytwo.jenkins.model.MavenJobWithDetails;
import org.apache.ibatis.jdbc.Null;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ServerServiceImpl implements ServerService {
    @Autowired
    private ServerMapper serverMapper;

    @Override
    public PageInfo getBaseServerForPaging(int pageIndex, int pageNum, String BaseServerType, String SystemType) {
        PageHelper.startPage(pageIndex, pageNum);
        String sql = "SELECT * from tbServerInfo where eStatus = 1 ";
        if (BaseServerType !=""){
            sql += " and vServerType = '"+ BaseServerType+"'";
        }
        if (SystemType !=""){
            sql += " and vSystem = '"+SystemType +"'";
        }

        List<Map<String,String>> list = serverMapper.findBaseServer(sql);
        PageInfo page = new PageInfo(list);
        return page;
    }
    @Override
    public PageInfo getbuildRecordForPaging(int pageIndex, int pageNum, String vServerName, String eStauts, String vLeader) {
        PageHelper.startPage(pageIndex, pageNum);
        vServerName = "%"+vServerName+"%";
        eStauts = "%"+eStauts+"%";
        vLeader = "%"+vLeader+"%";
        List<BuildRecordEntity> list = serverMapper.findbuildRecord(vServerName,eStauts,vLeader);
        for(BuildRecordEntity S : list){
            String  audit = S.geteAudit();
            if (audit.equals("2")){
                S.seteAudit("已审核");
            }else {
                S.seteAudit("待审核");
            }
        }
        PageInfo page = new PageInfo(list);
        return page;
    }


    @Override
    public int  Auditbuild(String buildId) throws IOException {
        BuildRecordEntity buildRecordEntity = serverMapper.findbuildApplyByID(buildId);
        String dev = buildRecordEntity.getvDev();
        String branch = buildRecordEntity.getvBranch();
        String servername = buildRecordEntity.getvServerName();
        String version = buildRecordEntity.getvVersion();
        if (buildRecordEntity.getServerType().contains("web服务")){
            try {
                JekinsServer2 server = new JekinsServer2(new URI("http://192.168.200.222:8888"), "jion", "jion");
                jobdetail2 jobWithDetails = server.getJob(servername);
                System.out.println(jobWithDetails.getUrl());
                jobWithDetails.setUrl("http://192.168.200.222:8888/job/"+servername+"/");
                if(jobWithDetails.isBuildable()) {
                    HashMap<String, String> params = new HashMap<String, String>();
                    params.put("env", dev);
                    params.put("build_type",dev);
                    params.put("BRANCH",branch);
                    jobWithDetails.build(params,false);
                    int result = serverMapper.AuditBuildApply(buildId);
                    return result;
                }

            } catch (Exception e) {
                return 1;
            }
        }else {
            try {
                JekinsServer2 server = new JekinsServer2(new URI("http://192.168.200.222:8888"), "jion", "jion");
                jobdetail2 jobWithDetails = server.getJob("go-publish");
                System.out.println(jobWithDetails.getUrl());
                jobWithDetails.setUrl("http://192.168.200.222:8888/job/"+"go-publish/");
                if(jobWithDetails.isBuildable()) {
                    HashMap<String, String> params = new HashMap<String, String>();
                    params.put("env", dev);
                    params.put("name",servername);
                    params.put("version",version);
                    jobWithDetails.build(params,false);
                    int result = serverMapper.AuditBuildApply(buildId);
                    return result;
                }

            } catch (Exception e) {
                return 1;
            }
        }

        return 1;
    }

    @Override
    public  int addBuildService(BuildRecordEntity buildRecordEntity){
        return serverMapper.addBuildService(buildRecordEntity);
    }
    @Override
    public  BaseServerEntity findOneBaseServer(String serverId){
        BaseServerEntity baseServerEntity = serverMapper.findOneById(serverId);
        String sql = "select * from tbDeployConf where deployID = "+baseServerEntity.geteDeploy();
        Map<String,String> map = serverMapper.Select_Sql_Only(sql);
        baseServerEntity.setvDeployType(map.get("eDeploy"));
        baseServerEntity.setvDeployType(map.get("vDeployUrl"));
        return baseServerEntity;
    }
    @Override
    public PageInfo LookDeployLog(int pageIndex, int pageNum,String vServerName){
        PageHelper.startPage(pageIndex, pageNum);
        List<BuildRecordEntity> list =  serverMapper.findRecordByServerName(vServerName);
        PageInfo page = new PageInfo(list);
        return page;
    }

    @Override
    public int updateServerService (BaseServerEntity baseServerEntity){
        return serverMapper.UpdateBaseServer(baseServerEntity);
    }

    @Override
    public PageInfo getServerTypeForPaging(int pageIndex, int pageNum) {
        PageHelper.startPage(pageIndex, pageNum);
       List<ComboBox> list = serverMapper.ComboBox_Server();

        PageInfo page = new PageInfo(list);
        return page;
    }
    @Override
    public PageInfo getLanguageType(int pageIndex, int pageNum) {
        PageHelper.startPage(pageIndex, pageNum);
        List<ComboBox> list = serverMapper.ComboBox_vLanguage();

        PageInfo page = new PageInfo(list);
        return page;
    }
        @Override
    public PageInfo getSystemType(int pageIndex, int pageNum) {
        PageHelper.startPage(pageIndex, pageNum);
        List<ComboBox> list = serverMapper.ComboBox_System();
        PageInfo page = new PageInfo(list);
        return page;
    }
    public PageInfo getProductType(int pageIndex, int pageNum) {
        PageHelper.startPage(pageIndex, pageNum);
        List<ComboBox> list = serverMapper.ComboBox_Product();

        PageInfo page = new PageInfo(list);
        return page;
    }
    public PageInfo getDeployType(int pageIndex, int pageNum) {
        PageHelper.startPage(pageIndex, pageNum);
        List<ComboBox> list = serverMapper.ComboBox_Deploy();
        PageInfo page = new PageInfo(list);
        return page;
    }

    //登录验证
    public LoginInfo getUserInfoSC(LoginInfo loginInfo){
        int result = serverMapper.GetUserInfoCount(loginInfo.getUsername());
        if (result > 0){
            LoginInfo login = serverMapper.GetUserInfoMp(loginInfo.getUsername());
            return  login;
        }else {
            LoginInfo lo = new  LoginInfo();
            lo.setPassword("用户名密码错误");
            return lo;
        }

    }
    //新增服务
    public int AddBaseServerSC(BaseServerEntity baseServerEntity){
        if (baseServerEntity.geteStatus().contains("on")){
            baseServerEntity.seteStatus("1");
        }else {baseServerEntity.seteStatus("-1");}
        //获取部署方式配置主键
        String eDeployID = serverMapper.getDeployFromConf(baseServerEntity.getvDeployType(),baseServerEntity.getvDeployUrl());
        baseServerEntity.seteDeploy(eDeployID);
        //获取开发编译配置
        String eLanguage = serverMapper.getLanguageFromConf(baseServerEntity.getvLanguage(),baseServerEntity.getvCompile());
        if (eLanguage != null){
        }else {
            System.out.println(eLanguage+"**************");
            int result = serverMapper.addLanguageConf(baseServerEntity.getvLanguage(),baseServerEntity.getvCompile());
            if (result>0){
                eLanguage = serverMapper.getLanguageFromConf(baseServerEntity.getvLanguage(),baseServerEntity.getvCompile());
            }
        }
        baseServerEntity.seteLanguage(eLanguage);

        int result =serverMapper.addBaseService(baseServerEntity);
        List<BaseServerEntity> list = serverMapper.CheckServer(baseServerEntity.getvServerName());
        if (list.size()>1){
            BaseServerEntity c = list.get(0);
            int result1 = serverMapper.deleteServer(c.getServerId());
            if (result1 > 0){
                return 1;
            }else {
                return 0;
            }
        }
        return result;
    }


}
